package com.ruoyi.quartz.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.quartz.domain.BillInfo;
import com.ruoyi.quartz.domain.TDevice;
import com.ruoyi.quartz.domain.TWareList;
import com.ruoyi.quartz.domain.WareEntry;
import com.ruoyi.quartz.mapper.TDeviceMapper;
import com.ruoyi.quartz.mapper.TWareListMapper;
import com.ruoyi.quartz.service.ITWareListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TWareListServiceImpl implements ITWareListService {
    @Autowired
    private TWareListMapper tWareListMapper;
    @Autowired
    private TDeviceMapper tDeviceMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param wareId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public TWareList selectTWareListByWareId(Long wareId) {
        return tWareListMapper.selectTWareListByWareId(wareId);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param tWareList 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TWareList> selectTWareListList(TWareList tWareList) {
        return tWareListMapper.selectTWareListList(tWareList);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param wareEntry 【请填写功能名称】
     * @return 结果
     */
    @Override

    public int insertTWareList(WareEntry wareEntry) {
        if (wareEntry != null) {
            List<BillInfo> billInfoList = wareEntry.getBillInfoList();
            TWareList tWareList = new TWareList();
            TDevice tDevice = new TDevice();

            //将发票list转为json格式数据，保存到tWareList的属性
            String jsonString = JSONObject.toJSONString(billInfoList);


            //保存入库表信息到tWareList对象
            BeanUtils.copyProperties(wareEntry, tWareList);
            //保存入库表信息到tWareList对象
            BeanUtils.copyProperties(wareEntry, tDevice);

            // 保存入库信息到设备
            tWareListMapper.insertTWareList(tWareList);
            tDeviceMapper.insertTDevice(tDevice);
            System.out.println("保存成功");
            return 1;

        } else {
            return -1;
        }
    }

    /**
     * 修改【入库】
     *
     * @param wareEntry 【入库】
     * @return 结果
     */
    @Override
    public int updateTWareList(WareEntry wareEntry) {

        List<BillInfo> billInfoList = wareEntry.getBillInfoList();
        TWareList tWareList = new TWareList();
        TDevice tDevice = new TDevice();

        //将发票list转为json格式数据，保存到tWareList的属性
        String billInfojson = JSON.toJSON(billInfoList).toString();
        wareEntry.setBillList(billInfojson);

        //保存入库表信息到tWareList对象
        BeanUtils.copyProperties(wareEntry, tWareList);
        //保存入库表信息到tDevice对象
        BeanUtils.copyProperties(wareEntry, tDevice);

        // 保存入库信息到设备
        tWareListMapper.insertTWareList(tWareList);
        tDeviceMapper.insertTDevice(tDevice);
        return 1;

    }


    /**
     * 批量删除【入库】
     *
     * @param wareIds 需要删除的【入库】主键
     * @return 结果
     */
    @Override
    public int deleteTWareListByWareIds(String wareIds) {
        return tWareListMapper.deleteTWareListByWareIds(Convert.toStrArray(wareIds));
    }

    /**
     * 删除【入库】信息
     *
     * @param wareId 【入库】主键
     * @return 结果
     */
    @Override
    public int deleteTWareListByWareId(Long wareId) {
        return tWareListMapper.deleteTWareListByWareId(wareId);
    }

    /**
     * 打印入库信息
     */


}